<?php

namespace app\order\logic;

use think\facade\Db;
use think\facade\Cache;
use util\Excel;
use think\exception\ValidateException;
use think\facade\Log;
use util\Token;
use util\Upload;
use think\cache\driver\Redis;
use app\api\logic\ApiOrderLogic;

class OrderPaymentToolsLogic
{

    public function __construct()
    {

    }
    
    /**
     * @desc    实时记录今天业绩
     * @date    2024/7/16
     * @author  zhangheng
     */
    public function index($params = [])
    {
        $limit   = $params['limit'] ?? 10;

        //实时时间搜索
        $date = $params['date'] ?? "";
        $customDate = $params['customDate'] ?? 0;

        $where = [];

        //customDate优先
        if(!empty($customDate)){
            switch ($customDate) {
                case '1':
                    //实时
                    $yesterday = date('Y-m-d', time() - timezone());  
                    $midnightYesterday = strtotime($yesterday . ' 00:00:00');
                    $endOfYesterday  = strtotime($yesterday . ' 23:59:59');
                    $where[]  = ['createtime', '>=', $midnightYesterday];
                    $where[]  = ['createtime', '<=', $endOfYesterday]; 

                    break;
                case '2':
                    $yesterday = date('Y-m-d', strtotime('-1 day') - timezone());
                    $midnightYesterday = strtotime($yesterday . ' 00:00:00');
                    $endOfYesterday  = strtotime($yesterday . ' 23:59:59');
                    $where[]  = ['createtime', '>=', $midnightYesterday];
                    $where[]  = ['createtime', '<=', $endOfYesterday]; 

                    break;
                case '3':
                    $yesterday = strtotime(date('Y-m-d', time() - timezone()));  
                    $yesterdayEnd = strtotime(date('Y-m-d', strtotime('-6 day') - timezone()));  
                    $where[] = ['createtime', '<=', $yesterday];
                    $where[] = ['createtime', '>=', $yesterdayEnd];

                    break;
                default:
                    $yesterday = date('Y-m-d', strtotime('-1 day') - timezone());
                    $midnightYesterday = strtotime($yesterday . ' 00:00:00');
                    $endOfYesterday  = strtotime($yesterday . ' 23:59:59');
                    $where[]  = ['createtime', '>=', $midnightYesterday];
                    $where[]  = ['createtime', '<=', $endOfYesterday]; 
            }
        }else{
            // 当自定义时间为空时 查日期有没有值
            if (!empty($date)) {
                $where[] = ['createtime', '>=', strtotime($date['0'])];
                $where[] = ['createtime', '<=', strtotime($date['1']) + 3600*24-1];

            }else{
                //实时
                $yesterday = date('Y-m-d', time() - timezone());  
                $midnightYesterday = strtotime($yesterday . ' 00:00:00');
                $endOfYesterday  = strtotime($yesterday . ' 23:59:59');
                $where = [];
                $where[]  = ['createtime', '>=', $midnightYesterday];
                $where[]  = ['createtime', '<=', $endOfYesterday];
            }
        }

        $where[]  = ['financial_status', 'in', ["partially_paid", "paid", "partially_refunded", "refunded"]];

        $lists = Db::name('order_achievement')
            ->field('SUM(total_price) as total_price,payment_gateway_names as tag_campaign')
            ->where($where)  
            ->group('payment_gateway_names')
            ->order('total_price desc')
            ->select()->toarray();
        
            // 计算总和  
        $totalSum = array_sum(array_column($lists, 'total_price'));  

        foreach ($lists as $k=>$v) {
            if(empty($v['tag_campaign'])){
                $lists[$k]['tag_campaign'] = "其他通道";
            }
            //占比
            $percentage = ($v['total_price'] / $totalSum) * 100;  
            $lists[$k]['percentage'] = round($percentage, 2)."%";  
        }

        $data = [
            'list' => $lists,
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }
}
